package com.bwda.dsrs.systemservice.controller;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.systemservice.annotations.NeedRole;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.condition.userrole.UserRoleCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.form.user.UserForm;
import com.bwda.dsrs.systemservice.domain.form.userrole.UserRoleCreateForm;
import com.bwda.dsrs.systemservice.domain.form.userrole.UserRoleQueryForm;
import com.bwda.dsrs.systemservice.domain.form.userrole.UserRoleUpdateForm;
import com.bwda.dsrs.systemservice.domain.po.userrole.UserRolePo;
import com.bwda.dsrs.systemservice.domain.vo.user.UserVo;
import com.bwda.dsrs.systemservice.domain.vo.userrole.UserRoleVo;
import com.bwda.dsrs.systemservice.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yangjun
 */
@RestController
@RequestMapping("/s/userrole")
@Api(value = "/s/userrole", tags = "用户角色表")
public class UserRoleController extends BaseController {

    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation(value = "查询用户角色表", notes = "查询用户角色表", httpMethod = "GET")
    @Loggable(descp = "查询用户角色", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<UserRoleVo> query(@ApiParam(value = "ID主键", required = true) @RequestParam Integer id) throws SystemServiceException {
        UserRolePo po = userRoleService.queryWithValid(id);
        UserRoleVo vo = CopyUtil.transfer(po, UserRoleVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "查询用户角色表数量", notes = "查询用户角色表数量", httpMethod = "GET")
    @Loggable(descp = "查询用户角色数量", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<Integer> queryCount(@ModelAttribute @Valid UserRoleQueryForm form) throws SystemServiceException {
        UserRoleCondition condition = this.getConditionByQueryForm(form);
        int count = userRoleService.queryCount(condition);
        return getSuccessResult(count);
    }

    @ApiOperation(value = "查询用户角色表列表", notes = "查询用户角色表列表", httpMethod = "GET")
    @Loggable(descp = "查询用户角色列", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreListResponse<UserRoleVo>> queryList(@ModelAttribute @Valid UserRoleQueryForm form) throws SystemServiceException {
        UserRoleCondition condition = this.getConditionByQueryForm(form);
        List<UserRolePo> poList = userRoleService.queryList(condition);
        List<UserRoleVo> voList = CopyUtil.transfer(poList, UserRoleVo.class);
        return getSuccessResult(getListResponse(voList));
    }


    @ApiOperation(value = "查询审批人", notes = "查询审批人", httpMethod = "GET")
    @Loggable(descp = "查询审批人", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryAudits", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreListResponse<UserForm>> queryAudits() throws SystemServiceException {

        String userCode = getUserCode();
       List<UserForm> userVoList= userRoleService.getAuditUserList( userCode);



        return getSuccessResult(getListResponse(userVoList));
    }

    @ApiOperation(value = "查询用户角色表列表(带分页)", notes = "查询用户角色表列表(带分页)", httpMethod = "GET")
    @Loggable(descp = "查询用户角色列(带分页)", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreCutPageResponse<UserRoleVo>> queryPageList(@ModelAttribute @Valid UserRoleQueryForm form) throws SystemServiceException {
        UserRoleCondition condition = this.getConditionByQueryForm(form);
        List<UserRoleVo> voList = new ArrayList<>();
        int count = userRoleService.queryCount(condition);
        if (count > 0) {
            List<UserRolePo> poList = userRoleService.queryList(condition);
            voList = CopyUtil.transfer(poList, UserRoleVo.class);
        }
        return getSuccessResult(getPageResponse(form, count, voList));
    }

    @NeedRole("sysadminrole")
    @ApiOperation(value = "新增用户角色表", notes = "新增用户角色表", httpMethod = "POST")
    @Loggable(descp = "新增用户角色", type = LogTypeEnum.CREATE, include = "")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<UserRoleVo> add(@ModelAttribute @Valid UserRoleCreateForm form) throws SystemServiceException {
        UserRolePo po = CopyUtil.transfer(form, UserRolePo.class);
        po.setId(1);
        userRoleService.insert(po);
        UserRoleVo vo = CopyUtil.transfer(po, UserRoleVo.class);
        return getSuccessResult(vo);
    }

    @NeedRole("sysadminrole")
    @ApiOperation(value = "修改用户角色表", notes = "修改用户角色表", httpMethod = "POST")
    @Loggable(descp = "修改用户角色", type = LogTypeEnum.UPDATE, include = "")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid UserRoleUpdateForm form) throws SystemServiceException {
        UserRolePo po = CopyUtil.transfer(form, UserRolePo.class);
        userRoleService.update(po);
        return getSuccessResult();
    }

    @NeedRole("sysadminrole")
    @ApiOperation(value = "删除用户角色", notes = "删除用户角色表", httpMethod = "POST")
    @Loggable(descp = "删除用户角色", type = LogTypeEnum.DELETE, include = "")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity delete(@ApiParam(value = "ID主键", required = true) @RequestParam Integer id) throws SystemServiceException {
        userRoleService.delete(id);
        return getSuccessResult();
    }

    /**
     * UserRoleQueryForm转换为UserRoleCondition
     *
     * @param form
     * @return
     */
    private UserRoleCondition getConditionByQueryForm(UserRoleQueryForm form) {
        UserRoleCondition condition = CopyUtil.transfer(form, UserRoleCondition.class);
        return condition;
    }

}